package com.oyjd.business.listview.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.oyjd.framework.util.FileOP;
import com.oyjd.framework.util.MathExtend;

/**
 * 统计工具方法
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class CountUtil {
	public static final String split = "\n";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String s = FileOP.readFile("D:/text.txt", "gb2312");
		String c = count(s);
		FileOP.writeFile("D:/a.html", c, "gb2312");
	}

	/**
	 * 初始化规则
	 * 
	 * @return
	 */
	private static List<TmpVO> initRules() {
		List<TmpVO> list = new ArrayList<TmpVO>();
		list.add(new TmpVO(0, 59, "不及格"));
		list.add(new TmpVO(60, 100, "及格"));
		for (int i = 0; i <= 8; i++) {
			int a = i * 10;
			int b = a + 9;
			list.add(new TmpVO(a, b, a + "~" + b));
		}
		list.add(new TmpVO(90, 100, "90~100"));
		return list;
	}

	/**
	 * 根据内容输出统计页面
	 * 
	 * @param c
	 * @return
	 */
	public static String count(String c) {
		StringBuffer errNums = new StringBuffer();
		int count = 0;
		int errCount = 0;
		String sum = "0";
		List<TmpVO> rules = initRules();
		List<String> fs = new ArrayList<String>();
		String[] ss = c.split(split);
		for (String s : ss) {
			if (null == s || "".equals(s.trim())) {
				continue;
			}
			try {
				sum = MathExtend.add(sum, s);
				float n = new BigDecimal(s).floatValue();
				if (n < 0 || n > 100) {
					errNums.append("[" + s + "]");
					errCount++;
				} else {
					for (TmpVO vo : rules) {
						if (n >= vo.getStart() && n < vo.getEnd() + 1) {
							vo.setCount(vo.getCount() + 1);
							vo.setCountSum(MathExtend.add(vo.getCountSum(), s));
						}
					}
					count++;
					fs.add(s);
				}
			} catch (Exception e) {
				errNums.append("[" + s + "]");
				errCount++;
			}
		}
		// 排序
		Collections.sort(fs, new Comparator<String>() {

			public int compare(String o1, String o2) {
				float n1 = new BigDecimal(o1).floatValue();
				float n2 = new BigDecimal(o2).floatValue();
				return n1 > n2 ? 1 : -1;
			}
		});

		// 格式化HTML输出
		StringBuffer sb = new StringBuffer();
		sb.append("<html>");
		if (errCount > 0) {
			sb.append("错误 ").append(errCount).append(" 个：").append(errNums).append("<br/><hr/>\r\n");
		}

		sb.append("<table width='100%' cellpadding='1' bgcolor='#FFA500'>\r\n");
		sb.append("<tr style='background-color:white;'>");
		sb.append("<th>总人数:" + count + "</th>");
		sb.append("<th>总分数:" + sum + "</th>");
		sb.append("<th>平均分:" + MathExtend.divide(sum, Integer.toString(count), 2) + "</th>");
		sb.append("</tr>\r\n");
		sb.append("<tr style='background-color:white;'>");
		sb.append("<th>最高分:" + fs.get(fs.size() - 1) + "</th>");
		sb.append("<th>最低分:" + fs.get(0) + "</th>");
		sb.append("<th>&nbsp;</th>");
		sb.append("</tr>\r\n");
		sb.append("</table><br/>\r\n");

		sb.append("<table width='100%' cellpadding='1' bgcolor='#FFA500'>\r\n");
		sb.append("<tr style='color:white;'>");
		sb.append("<th>分数段</th><th>人数</th><th>占比</th><th>总分</th><th>占比</th>");
		sb.append("</tr>\r\n");
		for (TmpVO vo : rules) {
			sb.append("<tr style='background-color:white;'>");
			sb.append("<th>" + vo.getNote() + "</th>");
			sb.append("<td>" + vo.getCount() + "</td>");
			sb.append("<td>" + MathExtend.divide(Integer.toString(vo.getCount() * 100), Integer.toString(count), 2) + "%</td>");
			sb.append("<td>" + vo.getCountSum() + "</td>");
			sb.append("<td>" + MathExtend.divide(Float.toString(Float.parseFloat(vo.getCountSum()) * 100), sum, 2) + "%</td>");
			sb.append("</tr>\r\n");
		}
		sb.append("</table><br/>\r\n");

		int wrap = 6;
		sb.append("<table width='100%' cellpadding='1' bgcolor='#FFA500'>\r\n");
		sb.append("<tr style='color:white;'>");
		for (int i = 1; i <= wrap; i++) {
			sb.append("<th width='" + 100f / wrap + "%'>" + i + "</th>");
		}
		sb.append("</tr>\r\n");
		for (int i = 0; i < fs.size(); i++) {
			String f = fs.get(i);
			if (i % wrap == 0) {
				sb.append("<tr style='background-color:white;'>");
			}
			sb.append("<td>" + f + "</td>");
			if (i % wrap == wrap - 1) {
				sb.append("</tr>\r\n");
			}
		}
		int p = fs.size() % wrap;
		if (p != 0) {
			for (int i = 0; i < wrap - p; i++) {
				sb.append("<td>&nbsp;</td>");
			}
			sb.append("</tr>\r\n");
		}
		sb.append("</table><br/><br/><br/>\r\n");

		sb.append("</html>");
		return sb.toString();
	}

	/**
	 * 临时值对象
	 * 
	 * @author 欧建东
	 * @email oyjd@163.com
	 * 
	 */
	private static class TmpVO {
		private int start;// 开始数字
		private int end;// 结束数字
		private String note;// 描述
		private int count;// 数量统计
		private String countSum = "0";// 总分数统计

		public TmpVO(int start, int end, String note) {
			super();
			this.start = start;
			this.end = end;
			this.note = note;
		}

		public int getStart() {
			return start;
		}

		public int getEnd() {
			return end;
		}

		public String getNote() {
			return note;
		}

		public int getCount() {
			return count;
		}

		public void setCount(int count) {
			this.count = count;
		}

		public String getCountSum() {
			return countSum;
		}

		public void setCountSum(String countSum) {
			this.countSum = countSum;
		}

	}
}
